﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Weiz.TaskManager.BLL;
using Weiz.TaskManager.Common;
using Weiz.TaskManager.Models;

namespace Weiz.Upload.Web.Controllers
{
    public class HomeController : Controller
    {
        ProductBLL bll = new ProductBLL();
        ResourcesBLL rbll = new ResourcesBLL();
        SpecificationsBLL sbll = new SpecificationsBLL();

        public ActionResult Index()
        {
            ViewBag.GUID = Guid.NewGuid();


            var selectItemList = new List<SelectListItem>() { 
                new SelectListItem(){Value="00000000-0000-0000-0000-000000000000",Text="选择",Selected=true}
            };
            var result = sbll.GetSpecificationsList(1, int.MaxValue);
            var selectList = new SelectList(result.Items, "ID", "Name");
            selectItemList.AddRange(selectList);
            ViewBag.Select = selectItemList;

            return View();
        }

        [ActionName("UploadImg")]
        public ActionResult UploadImg(RequestData data)
        {
            try
            {
                var oFile = Request.Files;
                for (int index = 0; index <= oFile.Count; index++)
                {
                    if (rbll.isExitsResources(oFile[index].FileName, data.Id))//
                        continue;
                    string code = rbll.GetFileNameCode(data.Id);
                    string[] fileInfo = oFile[index].FileName.Split('.');
                    string newFileName = data.Name + code + "." + fileInfo[1];

                    FileUploadMessage message = new FileUploadMessage()
                    {
                        FileData = oFile[index].InputStream,
                        FileName = newFileName,
                        SavePath = "/Product/Images/"

                    };
                    FileManageService file = new FileManageService();
                    string filePath = file.UploadFile(message);
                    ResourcesModel rModel = new ResourcesModel()
                    {
                        ID = Guid.NewGuid(),
                        Path = filePath,
                        ProductID = new Guid(data.Id),
                        Remark = "",
                        IsDelete = false,
                        FileName = oFile[index].FileName
                    };
                    var result = rbll.Add(rModel);
                }
                return Json(new { success = true }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new { success = false, msg = "图片上传失败" }, JsonRequestBehavior.AllowGet);
            }

        }

        [HttpPost]
        public ActionResult SaveProduct(Product model)
        {
            string msg = "";
            #region 判断商品是否存在
            var product = bll.GetProductById(model.ID.ToString());
            if (product != null)
                return Json(new { success = true, msg = msg, id = product.ID.ToString(), name = product.Name }, JsonRequestBehavior.DenyGet);
            product = bll.GetProductByName(model.Name);
            if (product != null)
                return Json(new { success = true, msg = msg, id = product.ID.ToString(), name = product.Name }, JsonRequestBehavior.DenyGet);
            #endregion

            //保存商品并返回商品的名称和唯一ID
            var result = bll.Add(model, out msg);
            return Json(new { success = result, msg = msg, id = model.ID.ToString(), name = model.Name }, JsonRequestBehavior.DenyGet);

        }

        public ActionResult Edit(string txt_name = "")
        {
            if (!string.IsNullOrEmpty(txt_name))
            {
                var model = new SpecificationsModel()
                {
                    ID = Guid.NewGuid(),
                    Name = txt_name,
                    IsDelete = false,
                    Remark = ""
                };
                sbll.Add(model);
            }
            var selectItemList = new List<SelectListItem>();
            var result = sbll.GetSpecificationsList(1, int.MaxValue);
            var selectList = new SelectList(result.Items, "ID", "Name");
            selectItemList.AddRange(selectList);
            ViewBag.Select = selectItemList;
            return View(result);
        }
    }

    public class RequestData
    {

        public string Name { get; set; }

        public string Id { get; set; }
    }
}
